#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_PAYLOAD_SIZE 8
typedef struct {
    int logical_channel_id;
    int physical_channel_id;
    unsigned char payload[MAX_PAYLOAD_SIZE];
    int SQ;
    int MFI;
} Frame;
void generate_payload_data(unsigned char *payload,int payload_size) {			
	*payload = 0;
	for (int i = 0; i < payload_size; i++) {
		payload[i] = rand()%2;
	}
}
void generate_data_file(int logical_channel_id, int physical_channel_id, long bandwidth,int S_Q,int MFI) {
	//printf("%d,%d,%d\n",logical_channel_id,physical_channel_id,data_source);
    unsigned char payload[MAX_PAYLOAD_SIZE];																						
    Frame frame;
    frame.SQ = S_Q;
    frame.MFI = MFI;
    frame.logical_channel_id=logical_channel_id;
    frame.physical_channel_id=physical_channel_id;
    generate_payload_data(frame.payload,sizeof(frame.payload));
    
    FILE* file;
    char filename[50];
    sprintf(filename, "DataFile_PHYSICALCHANNEL_%d.dat", physical_channel_id);	
    file = fopen(filename, "ab");											
    
    if (file != NULL) {
		fwrite(&frame,sizeof(Frame),1,file);
        fclose(file);
    	printf("Generated data file: %s\n", filename);
    } else {
        printf("Failed to create data file.\n");
    }
    return;
}
void read_combined_data_file() {
	int LID,PID,MFI;
	printf("请输入您像查询的数据文件\n");
	printf("物理通道id\n:");
	scanf("%d",&PID);
	char filename[50];
    sprintf(filename, "DataFile_PHYSICALCHANNEL_%d.dat", PID);
	FILE* file = fopen(filename, "rb");
    if (file == NULL) {
        printf("Failed to open the combined data file.\n");
        return;
    }
    Frame frame;
    while (fread(&frame, sizeof(Frame), 1, file) == 1) {      
        printf("Logical Channel ID: %d\n", frame.logical_channel_id);
        printf("Physical Channel ID: %d\n", frame.physical_channel_id);
        printf("Payload: ");
		for (int i = 0; i < sizeof(frame.payload); i++) {
	        printf("%d", frame.payload[i]);
	    }
        printf("\n");
        printf("SQ: %d\n", frame.SQ);
        printf("MFI: %d\n", frame.MFI);
        printf("\n");
    }
    
    fclose(file);
}
void read_configFile(char *order,int *mode){
	const char *configFile = "config.txt";
	FILE *file = fopen(configFile, "r");
    if (file == NULL) {
        printf("无法打开配置文件 %s\n", configFile);
    }
    char line[100];
    while (fgets(line, 100, file) != NULL) {
        if (line[0] == '\n' || line[0] == '#') {
            continue;
        }
        char *line_copy = strdup(line);
        char *key = strtok(line_copy, "=");
        char *value = strtok(NULL, "=");

        key[strcspn(key, " \t\n")] = '\0';
        value[strcspn(value, " \t\n")] = '\0';

        if (strcmp(key, "order") == 0) {
            strcpy(order, value);
        } else if (strcmp(key, "mode") == 0) {
            *mode = atoi(value);
        }
        free(line_copy);
    }
    fclose(file);
    printf("order: %s\n", order);
    printf("mode: %d\n", *mode);
}
void clear_data_file(int physical_channel_id) {
    char filename[50];
    sprintf(filename, "DataFile_PHYSICALCHANNEL_%d.dat", physical_channel_id);
    
    FILE* file = fopen(filename, "wb");
    if (file != NULL) {
        fclose(file);
        printf("Cleared data file: %s\n", filename);
    } else {
        printf("Failed to clear data file.\n");
    }
}
int main(){
	srand(time(0));  
	int MFI = 0;
	int data_source = 0;
	int logical_channel_id = 0;
    int physical_channel_id = 0;
    long vc4_bandwidth = 2500000000; 
    long vc12_bandwidth = 500000000; 
    char order[10];
    int mode;
    int logical_channel_sign = 0;
    read_configFile(order,&mode);
    
    if(strcmp(order,"combination")==0){
    	int n, a_sum, b_sum, a_count, b_count;
    	n = rand() % 64 + 1;
	
        a_sum = n;
	    b_sum = (64 - n) * 63;
        srand(time(0));
	    a_count = rand() % (a_sum + 1); 				  // 生成0到a_sum之间的随机数
	    b_count = rand() % (b_sum + 1);  				  // 生成0到b_sum之间的随机数
	    int a_array[a_count];
	    int b_array[b_count];
	
	    for (int i = 0; i < a_count; i++) {
	        if (i == a_count - 1) {
	        	a_array[i]=a_sum;
	    } else {
	        int max_random_a = a_sum - (a_count - i);  	  // 随机数上限为剩余总和减去剩余数量之和
            int a = rand() % (max_random_a + 1) + 1; 	  // 随机生成一个小于等于max_random_a的数
	        a_sum -= a;
			a_array[i]=a; 							   	 
	        }
	    }
	    for (int i = 0; i < b_count; i++) {
	        if (i == b_count - 1) {
	            b_array[i]=b_sum;
	        } else {
	            int max_random_b = b_sum - (b_count - i); // 随机数上限为剩余总和减去剩余数量之和
	            int b = rand() % (max_random_b + 1) + 1;  // 随机生成一个小于等于max_random_b的数
	            b_sum -= b;                               // 更新b的总和
	            b_array[i]=b; 							   	 
	        }
	    }
		
    	if(mode==0){
    		for(int j=0;j<a_count;j++){
	   			for(int i=0;i<a_array[j];i++){
	   				generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth,i,0);
	   				physical_channel_id++;
			  		}
			   	logical_channel_id++;
			}
			for(int j=0;j<b_count;j++){
   				for(int i=0;i<b_array[j];i++){
   					generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth,i,0);	
   					physical_channel_id++;
		   		}
		   		logical_channel_id++;
			}
		}else if(mode>0&&mode<=4095){
			for(MFI=1;MFI<=mode;MFI++){
				for(int j=0;j<a_count;j++){
	   				for(int i=0;i<a_array[j];i++){
	   					generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth,i,MFI);
	   					physical_channel_id++;
			  		}
			   		logical_channel_id++;
				}
				for(int j=0;j<b_count;j++){
   					for(int i=0;i<b_array[j];i++){
   						generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth,i,MFI);	
   						physical_channel_id++;
		   			}
		   			logical_channel_id++;
				}
				logical_channel_id=0;
				physical_channel_id=0;
			}
		}else{
			printf("输入模式错误！");
		}
   		
	}
    else if(strcmp(order,"VC-4")==0){
    	if(mode>0&&mode<=4095){
    		for(MFI=1;MFI<=mode;MFI++){
    		for(physical_channel_id=0;physical_channel_id<=64;physical_channel_id++){
    			logical_channel_id=physical_channel_id;
    			generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth,1,MFI);
				}
			}
		}else if(mode==0){
			for(physical_channel_id=0;physical_channel_id<=6;physical_channel_id++){
    			logical_channel_id=physical_channel_id;
    			generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth,1,MFI);
			}
		}else{
			printf("输入模式错误！");
		}
	}
    else if(strcmp(order,"VC-12")==0){
    	if(mode>0&&mode<=4095){
    		for(MFI=1;MFI<=mode;MFI++){
    		for(physical_channel_id=0;physical_channel_id<=4032;physical_channel_id++){
    			logical_channel_id=physical_channel_id;
    			generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth,1,0);
			}
				if(MFI==4095){
					MFI=0;
				}
			}
		}else if(mode==0){
			for(physical_channel_id=0;physical_channel_id<=4032;physical_channel_id++){
    			logical_channel_id=physical_channel_id;
    			generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth,1,0);
			}
		}else{
			printf("输入模式错误！");
		}
	}
	else if(strcmp(order,"read")==0){
		int file_count = 64;
    	read_combined_data_file();
	}
	else if(strcmp(order,"clear")==0){
		for(int i = 0 ; i <= 4095 ; i++){
			clear_data_file(i);
		}
	}
	else {
		printf("无效输入啊哥们!\n");
	}       
	printf("\n");
    return 0;
}